package com.chookss.tiku

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.tiku.entity.Subject
import com.chookss.tiku.entity.LiveResult
import com.chookss.tiku.response.BaseResponse
import com.chookss.tiku.response.PracticeSubjectList
import com.chookss.tools.Utils
import com.chookss.tools.httpForm

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2020/12/28
 * Note: 模拟试卷数据类
 */
class MyCollectSubjectViewModel() : BasePracticeTestViewModel() {
    private val pageSize = 50
    //题目列表
    var subjectList = mutableListOf<Subject>()
    lateinit var memoryLvl: String
    lateinit var subjectTypeCode: String
    lateinit var sourceType: String
    private fun createSubjectResult(subjectList: List<Subject>, listIndex: Int): LiveResult<Subject> {
        if (subjectList.size > listIndex) {
            return LiveResult(subjectList[listIndex])
        } else {//没有结果,结束了
            return LiveResult(null)
        }
    }

    //取消收藏题目
     fun collectSubject(subject: Subject, sourceType:String, collectStatus:String):LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()

//        collectType	true	String	收藏类型 0 收藏视频 1 收藏题目
//        collectId	true	String	视频id或者题目编码
//        collectStatus	true	String	状态 0 取消 1 收藏

        val params2: MutableMap<String, Any?> = HashMap()
        params2["collectType"] = "1"
        params2["collectId"] = subject.subjectCode
        params2["collectStatus"] = collectStatus
        params2["sourceType"] = sourceType

        jobs + httpForm<BaseResponse> {
            url = "api/sys/my/toCollect"
            params = params2
            onSuccess = {
                subject.collectFlag = collectStatus
                liveResult.postValue(LiveResult(null))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    private fun deleteSubjectCache(subject: Subject):Boolean {
        if (subjectList == null) {
            return false
        }
        for (i in (subjectList.size-1) downTo 0) {
            if (subjectList[i].subjectCode == subject.subjectCode) {
                subjectList.removeAt(i)
                return true
            }
        }
        return false
    }

    //下载题目列表
    override fun getSubject(index: Int): LiveData<LiveResult<Subject>> {
        var liveResult = MutableLiveData<LiveResult<Subject>>()
        var pageNum = index / pageSize
        if(index!=0){
            if ( index< subjectList.size) {
                liveResult.postValue(createSubjectResult(subjectList, index))
                return liveResult
            }
        }
        val params2: MutableMap<String, Any?> = HashMap()
        if (!Utils.isNull(memoryLvl)) {
            params2["memoryLvl"] = memoryLvl
        }
        if (!Utils.isNull(subjectTypeCode)) {
            params2["subjectTypeCode"] = subjectTypeCode
        }
        if (!Utils.isNull(sourceType)) {
            params2["sourceType"] = sourceType
        }
        params2["pageNum"] = (pageNum + 1).toString()
        params2["pageSize"] =pageSize.toString()
        jobs + httpForm<PracticeSubjectList.Response> {
            url = "api/subject/getCollectSubject"
            params = params2
            onSuccess = {
                if(pageNum==0){
                    subjectList.clear()
                }
                subjectList.addAll(it.data ?: emptyList())
                liveResult.postValue(createSubjectResult(subjectList, index))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }


    //提交答案
    override fun submitAnswer(subject: Subject, answers: List<String>, time: Long) = submitErrorSubject(subject, answers)

}
